package where2go.sctu.com.zhiyou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import where2go.sctu.com.zhiyou.MainActivity;
import where2go.sctu.com.zhiyou.MyApplication;
import where2go.sctu.com.zhiyou.R;
import where2go.sctu.com.zhiyou.entity.Login;
import where2go.sctu.com.zhiyou.utils.ShareUtils;

/**
 * ProjectName: zhiyou
 * PackageName: where2go.sctu.com.zhiyou.activity
 * Created by 张某人
 * CreatedTime:on 2018/5/24   23:36
 * Description:  登录
 */

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView btnBack;
    private EditText etUserName;
    private EditText etUserPassword;
    private Button btnLogin;
    private Button btnForget;
    private Button btnRegister;

    private Login login = new Login();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        postData();
    }

    private void initView() {
        btnBack = (ImageView) findViewById(R.id.user_back);
        btnBack.setOnClickListener(this);

        etUserName = (EditText) findViewById(R.id.login_username);
        etUserPassword = (EditText) findViewById(R.id.login_password);
        btnLogin = (Button) findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(this);

        btnForget = (Button) findViewById(R.id.forget_password);
        btnForget.setOnClickListener(this);

        btnRegister = (Button) findViewById(R.id.login_register);
        btnRegister.setOnClickListener(this);
    }

    private void postData() {
        String url = "http://47.104.80.155/youyiyou/users/login?";
        StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                Type type = new TypeToken<Login>() {
                }.getType();
                login = gson.fromJson(response, type);
                int error = login.getError();
                //判断错误码
                if (error == 0) {
                    ShareUtils.putString(LoginActivity.this,"token",login.getData());
                    Toast.makeText(LoginActivity.this, "登录成功",
                            Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    finish();
                } else if (error == 108) {
                    Toast.makeText(LoginActivity.this, login.getMsg(),
                            Toast.LENGTH_SHORT).show();
                } else if (error == 107) {
                    Toast.makeText(LoginActivity.this, login.getMsg(),
                            Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(LoginActivity.this, login.getMsg(),
                            Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(LoginActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("username", etUserName.getText().toString().trim());
                map.put("password", etUserPassword.getText().toString().trim());
                return map;
            }
        };
        request.setTag("loginPost");
        MyApplication.getHttpQueues().add(request);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                //获取输入框的值
                String username = etUserName.getText().toString().trim();
                String password = etUserPassword.getText().toString().trim();
                if (!TextUtils.isEmpty(username) & !TextUtils.isEmpty(password)) {
                    postData();
                } else {
                    Toast.makeText(LoginActivity.this, "用户名或密码不能为空!",
                            Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.login_register:
                startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
                break;
            case R.id.forget_password:
                startActivity(new Intent(LoginActivity.this,ForgetPasswordActivity.class));
                break;
            case R.id.user_back:
                finish();
                break;
            default:
                break;
        }
    }
}
